package v2

import (
	"github.com/silenceper/wechat/v2"
	"github.com/silenceper/wechat/v2/cache"
	"github.com/silenceper/wechat/v2/miniprogram"
	miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
	"github.com/silenceper/wechat/v2/officialaccount"
	oConfig "github.com/silenceper/wechat/v2/officialaccount/config"
	offConfig "github.com/silenceper/wechat/v2/officialaccount/config"
	"github.com/silenceper/wechat/v2/pay"
	cfg "github.com/silenceper/wechat/v2/pay/config"
	"github.com/silenceper/wechat/v2/pay/notify"
	"github.com/silenceper/wechat/v2/pay/transfer"
	"xialou_platform/app/errorx"
	"xialou_platform/app/wxpay"
)

type WxClient struct {
	config          *wxpay.WxPayConf
	Pay             *pay.Pay
	Transfer        *transfer.Transfer
	Notify          *notify.Notify
	MiniProgram     *miniprogram.MiniProgram
	OfficialAccount *officialaccount.OfficialAccount
}

func NewWxClient(config *wxpay.WxPayConf, redisConf *cache.RedisOpts, p, t, n, m bool) *WxClient {
	client := &WxClient{
		config: config,
	}
	c := &cfg.Config{
		MchID:     config.MchId,
		AppID:     config.AppId,
		Key:       config.APIv2Key,
		NotifyURL: config.NotifyUrl,
	}
	if p {
		client.Pay = pay.NewPay(c)
	}
	if t {
		client.Transfer = transfer.NewTransfer(c)
	}
	if n {
		client.Notify = notify.NewNotify(c)
	}
	if m {
		miniCfg := &miniConfig.Config{
			AppID:     config.AppId,
			AppSecret: config.AppSecret,
			Cache:     cache.NewRedis(redisConf),
		}
		client.MiniProgram = miniprogram.NewMiniProgram(miniCfg)
	}
	client.OfficialAccount = officialaccount.NewOfficialAccount(&offConfig.Config{
		AppID:     config.AppId,
		AppSecret: config.AppSecret,
		Cache:     cache.NewRedis(redisConf),
	})
	return client
}

// NewStoreWxClient 商家微信客户端
// config 微信信息配置
// p 微信支付 true 初始化
// t 付款到零钱
// n 回调
// m 小程序
func NewStoreWxClient(config *wxpay.WxPayConf, wxPay, wxTransfer, wxNotify, wxMiniProgram, wxOfficialAccount bool) *WxClient {
	client := &WxClient{}
	c := &cfg.Config{
		MchID:     config.MchId,
		AppID:     config.StoreAppId,
		Key:       config.APIv2Key,
		NotifyURL: config.NotifyUrl,
	}
	wechat.NewWechat()
	if wxPay {
		client.Pay = pay.NewPay(c)
	}
	if wxTransfer {
		client.Transfer = transfer.NewTransfer(c)
	}
	if wxNotify {
		client.Notify = notify.NewNotify(c)
	}
	if wxMiniProgram {
		miniCfg := &miniConfig.Config{
			AppID:     config.StoreAppId,
			AppSecret: config.StoreAppSecret,
			Cache:     cache.NewMemory(),
		}
		client.MiniProgram = miniprogram.NewMiniProgram(miniCfg)
	}
	if wxOfficialAccount {
		oCfg := &oConfig.Config{
			AppID:     config.StoreAppId,
			AppSecret: config.StoreAppSecret,
			Cache:     cache.NewMemory(),
		}
		officialaccount.NewOfficialAccount(oCfg)
	}
	return client
}

func (wxClient *WxClient) DealWxPay(partnerTradeNo string, openId string, money int64) error {

	resp, err := wxClient.Transfer.WalletTransfer(&transfer.Params{
		PartnerTradeNo: partnerTradeNo,
		OpenID:         openId,
		CheckName:      false,
		Amount:         int(money),
		Desc:           "下楼健身吧钱包提现",
		RootCa:         wxClient.config.RootCa,
	})
	if err != nil {
		return errorx.NewDefaultError(resp.ReturnMsg)
	}
	return err
}
